package engine.ui.equipStrenGthen 
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.View;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.ui.NewItemChangeUI;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import newx.BaseObject;
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.group.NxViewGroup;
	import newx.com.control.NxClip;
	import newx.com.control.radio.NxRadio;
	import newx.com.control.radio.NxRadioEvent;
	import newx.com.control.radio.NxRadioGroup;
	import newx.com.control.text.NxLabel;
	/**
	 * ...
	 * @author ...WangHaiBo 
	 * ...........www.woniu.com
	 * 装备拆解
	 */
	public class EquipUnenchaseLogic implements IEquipLogic 
	{
		private static var _self			:EquipUnenchaseLogic;
		private var viewgroup_left_4		:NxViewGroup;
		private var hole1_select_4			:NxRadio;
		private var hole2_select_4			:NxRadio;
		private var hole3_select_4			:NxRadio;
		private var hole4_select_4			:NxRadio;
		private var hole1_lab_4				:NxLabel;
		private var hole2_lab_4				:NxLabel;
		private var hole3_lab_4				:NxLabel;
		private var hole4_lab_4				:NxLabel;
		private var lab_succeedpercent_4	:NxLabel;
		private var lab_needmoney_4			:NxLabel;
		private var btn_star_4				:NxButton;
		
		private var Jewel0					:int;
		private var Jewel1					:int;
		private var Jewel2					:int;
		private var Jewel3					:int;
		private var holeCount				:int
		private var radioGroup				:NxRadioGroup;
		private var _viewData				:BaseObject	=	null;
		private var _bindManage				:BindManage;
		private var _model					:ModelLocator
		public function EquipUnenchaseLogic(val:MovieClip) 
		{
			viewgroup_left_4		=	val.getChildByName("viewgroup_left_4") as NxViewGroup;
			hole1_select_4			=	val.getChildByName("hole1_select_4") as NxRadio;
			hole2_select_4			=	val.getChildByName("hole2_select_4") as NxRadio;
			hole3_select_4			=	val.getChildByName("hole3_select_4") as NxRadio;
			hole4_select_4			=	val.getChildByName("hole4_select_4") as NxRadio;
			hole1_lab_4				=	val.getChildByName("hole1_lab_4") as NxLabel;
			hole2_lab_4				=	val.getChildByName("hole2_lab_4") as NxLabel;
			hole3_lab_4				=	val.getChildByName("hole3_lab_4") as NxLabel;
			hole4_lab_4				=	val.getChildByName("hole4_lab_4") as NxLabel;
			lab_succeedpercent_4	=	val.getChildByName("lab_succeedpercent_4") as NxLabel;
			lab_needmoney_4			=	val.getChildByName("lab_needmoney_4") as NxLabel;
			btn_star_4				=	val.getChildByName("btn_star_4") as NxButton;
			radioGroup				=	new NxRadioGroup([hole1_select_4, hole2_select_4, hole3_select_4, hole4_select_4]);
			_bindManage				=	BindManage.getInstance();
			_model					=	ModelLocator.getInstance();
			addListent();
		}
		public static function getInstance(dis:MovieClip):EquipUnenchaseLogic
		{
			if (_self == null)
			{
				_self	=	new EquipUnenchaseLogic(dis);
			}
			return _self;
		}
		/**
		 * 添加装备
		 * @param	val
		 */
		public function addEquip(val:BaseObject):void
		{
			var item:MovieClip	=	viewgroup_left_4.getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			reduction();
			_viewData				=	val;
			var equipClip:NxClip	=	new NxClip();
			equipClip.width			=	equipClip.height	=	40;
			equipClip.x				=	6;
			equipClip.y				=	5;
			val.HandleProperties("NowViewType",View.VIEWPORT_UNENCHASE)
			item["data"]			=	val;
			equipClip.source		=	"res/items/icon_" + val.GetProperties("Photo") + ".jpg";
			item.addChild(equipClip);
			addBind();
			needMenoy();
			succeedPercent();
			checkStarBtnEnabled();
		}
		
		/**
		 * 删除装备
		 * @param	val
		 */
		public function removeEquip(val:BaseObject):void
		{
			var item:MovieClip	=	viewgroup_left_4.getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			reduction();
			removeBind();
			item["data"]			=	null;
			_viewData				=	null;
			needMenoy();
			succeedPercent();
			checkStarBtnEnabled();
			
		}
		/**
		 * 清空所有的显示信息
		 */
		public function removeAll():void
		{
			var item:MovieClip	=	viewgroup_left_4.getItemByIndex(0);
			while (item.numChildren > 1)
			{
				item.removeChildAt(1);
			}
			reduction();
			item["data"]			=	null;
			_viewData				=	null;
			lab_needmoney_4.htmlText	=	"";
			lab_succeedpercent_4.htmlText	=	""
			
		}
		/**
		 * 添加绑定
		 */
		private function addBind():void
		{
			removeBind();
			if (_viewData == null) return;
			Jewel0		=	_bindManage.BindProp(_viewData as ObjectData, "Jewel0", 	this, "fristLabelData");
			Jewel1		=	_bindManage.BindProp(_viewData as ObjectData, "Jewel1", 	this, "secondLabelData");
			Jewel2		=	_bindManage.BindProp(_viewData as ObjectData, "Jewel2", 	this, "threeLabelData");
			Jewel3		=	_bindManage.BindProp(_viewData as ObjectData, "Jewel3", 	this, "fourLabelData");
			holeCount	=	_bindManage.BindProp(_viewData as ObjectData, "HoleCount", 	this, "holeCountlData");
		}
		/**
		 * 删除绑定
		 */
		private function removeBind():void
		{
			if (_viewData == null) return;
			_bindManage.UnbindProp(_viewData as ObjectData, "fristLabelData", 	Jewel0);
			_bindManage.UnbindProp(_viewData as ObjectData, "secondLabelData", Jewel1);
			_bindManage.UnbindProp(_viewData as ObjectData, "threeLabelData", 	Jewel2);
			_bindManage.UnbindProp(_viewData as ObjectData, "fourLabelData", 	Jewel3);
			_bindManage.UnbindProp(_viewData as ObjectData, "holeCountlData",	holeCount);
			
		}
		/**
		 * 孔洞一
		 */
		public function set fristLabelData(val:String):void
		{
			showEquipProperty(val, 0, hole1_lab_4, hole1_select_4);
		}
		/**
		 * 孔洞二
		 */
		public function set secondLabelData(val:String):void
		{
			showEquipProperty(val, 1, hole2_lab_4, hole2_select_4);
		}
		/**
		 * 孔洞三
		 */
		public function set threeLabelData(val:String):void
		{
			showEquipProperty(val, 2, hole3_lab_4, hole3_select_4);
		}
		/**
		 * 孔洞四
		 */
		public function set fourLabelData(val:String):void
		{
			showEquipProperty(val, 3, hole4_lab_4, hole4_select_4);
		}
		/**
		 * 孔洞数
		 */
		public function set holeCountlData(val:String):void
		{
			needMenoy();
		}
		/**
		 * 显示装备的特殊属性
		 * @param	holeVal_
		 * @param	holeNum_
		 * @param	label_
		 * @param	pic_
		 */
		private function showEquipProperty(holeVal_:String,holeNum_:int,label_:NxLabel,radio_:NxRadio):void
		{
			var eqType:String	=	"";

			
			if (holeVal_ == "-1") 
			{
				label_.color			=	0x00FF00
				eqType					=	StaticIni.getSwfLangStr("F_Gem_13234")
				radio_.visible			=	false;
			}
			else if (holeVal_ == "0") 
			{
				label_.color			=	0xFF0000
				eqType					=	StaticIni.getSwfLangStr("F_Gem_13245")
				radio_.visible			=	false;
			}
			else
			{	
				if (_viewData != null)
				{
					label_.color		=	0xFFFF00
					eqType				=	NewItemChangeUI.getInstance().readSpecialProperty(holeNum_,_viewData as ObjectData);
					radio_.visible		=	true;
				}
			}
			label_.text	=	eqType;
		}
		/**
		 * 初始化
		 */
		private function reduction():void
		{
			hole1_select_4			.visible		=	false;
			hole2_select_4			.visible		=	false;
			hole3_select_4			.visible		=	false;
			hole4_select_4			.visible		=	false;
			hole1_lab_4				.text			=	"";
			hole2_lab_4				.text			=	"";
			hole3_lab_4				.text			=	"";
			hole4_lab_4				.text			=	"";
			lab_succeedpercent_4	.text			=	"";
			lab_needmoney_4			.text			=	"";
			if (radioGroup.selectedNxRadio != null) 
			{
				radioGroup.selectedNxRadio.selected		=	false;
			}
			radioGroup.reset();	
			btn_star_4.enabled				=	false;
		}
		/**
		 * 拆解时所要的钱数
		 * @param	val
		 */
		private function needMenoy():void
		{
			var tempMoney:int	=	0;
			var menoyStr:String	=	"";
			if(_viewData	!= null)
			{
				if(_viewData.CheckProperties("NeedLevel"))
				{
					var needLevel:int		=	_viewData.GetProperties("NeedLevel");
					if(radioGroup.selectedIndex > -1)
					{
						if (radioGroup.selectedIndex < 3) 
						{
							tempMoney	=	needLevel * 100 + 1000;
						}
						else 
						{
							tempMoney	=	3* (needLevel * 100 + 1000);
						}
					}
				}
				menoyStr		=	String(tempMoney);
				if (tempMoney <= _model.scene.Role.getGoldCoin()) 
				{
					menoyStr	=	"<font color='#00FF40'>"	+	menoyStr	+	"</font>"
				}
				else
				{
					menoyStr	=	"<font color='#FF0000'>"	+	menoyStr	+	"</font>"
				}
				lab_needmoney_4.htmlText		=	StaticIni.getSwfLangStrVar("F_Gem_13222", [menoyStr])	+	StaticIni.getSwfLangStr("CapitalType_0");
			}
		}
		/**
		 * 成功率
		 */
		private function succeedPercent():void
		{
			var succeedStr:String	=	"";
			if(_viewData	!= null)
			{
				succeedStr	=	"<font color='#00FF40'>100%</font>";
				lab_succeedpercent_4.htmlText	=	StaticIni.getSwfLangStrVar("NewItemChangeUIStr_36", [succeedStr]);
			}
			else
			{
				lab_succeedpercent_4.htmlText	=	succeedStr;
			}
			
		}
		/**
		 * 按钮是否可用
		 */
		private function checkStarBtnEnabled():void
		{
			if (_viewData != null) 
			{
				btn_star_4.enabled	=	true;
			}
			else
			{
				btn_star_4.enabled	=	false;
			}
		}
		/**
		 * 开始拆解
		 * @param	e
		 */
		private function starBtnClickHandle(e:MouseEvent):void
		{
			var rockIndex:int	=	radioGroup.selectedIndex;
			if (rockIndex < 0)return 
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_UNENCHASE,0,rockIndex);
		}
		/**
		 * 单选点击事件
		 * @param	e
		 */
		private function radioClickHandle(e:NxRadioEvent):void
		{
			if(_viewData != null)
			{
				needMenoy();
			}
		}
		/**
		 * 添加事件
		 */
		private function addListent():void
		{
			btn_star_4.addEventListener(MouseEvent.CLICK, starBtnClickHandle);
			radioGroup.addEventListener(NxRadioEvent.RADIO_GROUP_CLICK, 		radioClickHandle);

		}
		/**
		 * 删除事件
		 */
		private function removeListent():void
		{
			btn_star_4.removeEventListener(MouseEvent.CLICK, starBtnClickHandle);
		}
		
		
	}

}